package com.earl.eatbao.web;

import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;

import com.earl.eatbao.commons.vo.ResultMessage;
import com.earl.eatbao.domain.eatbao.Comment;
import com.earl.eatbao.service.CommentService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author Administrator
 * @version 1.0
 * @created 23-十一月-2016 16:55:14
 */
@Controller
@RequestMapping("/comment")
public class CommentController extends BaseController {

	   private final Logger logger = LoggerFactory.getLogger(CommentController.class);

	    @Autowired
	    private CommentService commentService;
	    
	/**
	 * 删除非法评论
	 * @param commentId
	 */
	@ApiOperation(value = "删除非法评论", notes = "删除非法评论", httpMethod = "DELETE", response = String.class)
	@RequestMapping(value="/delete/{commentId}",  method = RequestMethod.DELETE)
	public ResponseEntity<ResultMessage> delete(
	        @ApiParam(name = "commentId", required = true, value = "要删除评论的ID")
	        @PathVariable
	        Long commentId){

		logger.info("<====== 进入delete:方法,{[commentId] :" + commentId + " } ====");
		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		result.setServiceResult(commentService.deleteComments(commentId));
		result.setMessageCode(200l);
		if (result.getServiceResult()) {
			result.setResultInfo("请求操作成功");
		} else {
			result.setResultInfo("请求操作失败");
		}

		logger.info("==== 退出delete方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	/**
	 *  查询所有评论
	 */
	@ApiOperation(value = "查看所用评论", notes = "查看所用评论", httpMethod = "GET", response = String.class)
	@RequestMapping(value="/all",  method = RequestMethod.GET)
	public ResponseEntity<ResultMessage> findAllComment(){
		logger.info("<====== 进入findAllComment:方法,{[] : } ====");
		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		result.getResultParm().put("commentList",commentService.findAll());
		result.setMessageCode(200l);
		if (null != result.getResultParm().get("commentList")) {
			result.setResultInfo("请求操作成功");
		} else {
			result.setResultInfo("请求操作失败");
		}

		logger.info("==== 退出findAll方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	
	/**
	 *  查询指定订单评论
	 * @param orderId
	 */
	@ApiOperation(value = "查询指定订单评论", notes = "查询指定订单评论", httpMethod = "GET", response = String.class)
	@RequestMapping(value="/findByOrderId/{orderId}",  method = RequestMethod.GET)
	public ResponseEntity<ResultMessage> findOrderComment(
	        @ApiParam(name = "orderId", required = true, value = "订单ID")
	        @PathVariable Long orderId){

		logger.info("<====== 进入findByOrderId:方法,{[orderId] :" + orderId + " } ====");
		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		result.getResultParm().put("comment", commentService.findOneByOrderId(orderId));
		result.setMessageCode(200l);
		if (null != result.getResultParm().get("comment")) {
			result.setResultInfo("请求操作成功");
		} else {
			result.setResultInfo("请求操作失败");
		}

		logger.info("==== 退出findByOrderId方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	
	/**
	 * 评论订单
	 * @param comment 评论实体
	 * @return
	 */
	@ApiImplicitParams({
	            @ApiImplicitParam(name = "orderId",value = "订单编号ID", required = true, dataType = "Long", paramType = "Query"),
	            @ApiImplicitParam(name = "userId",value = "用户ID", required = true, dataType = "Long", paramType = "Query"),
	            @ApiImplicitParam(name = "businessId",value = "对方用户ID", required = true, dataType = "Long", paramType = "Query"),
	            @ApiImplicitParam(name = "content",value = "评论内容", required = true, dataType = "Stirng", paramType = "Query")
	           })
    @ApiOperation(value = "提交评论", notes = "提交评论", httpMethod = "POST", response = String.class)
    @RequestMapping(value="/commentOrders",  method = RequestMethod.POST)
	public ResponseEntity<ResultMessage> commentOrders(Comment comment){
        logger.info("<====== 进入commentOrders:方法,{[comment] :" + comment.toString() + " } ====");

		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		result.setServiceResult(commentService.commentsOrders(comment));
		result.setMessageCode(200l);
		if (result.getServiceResult()) {
			result.setResultInfo("请求操作成功");
		} else {
			result.setResultInfo("请求操作失败");
		}

		logger.info("==== 退出commentOrders方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}


    /**
     * 回复评论
     * @param comment
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId",value = "订单编号ID", required = true, dataType = "Long", paramType = "Query"),
            @ApiImplicitParam(name = "froms",value = "用户ID", required = true, dataType = "Long", paramType = "Query"),
            @ApiImplicitParam(name = "tos",value = "对方用户ID", required = true, dataType = "Long", paramType = "Query"),
            @ApiImplicitParam(name = "content",value = "评论内容", required = true, dataType = "Stirng", paramType = "Query")
    })
    @ApiOperation(value = "回复评论", notes = "回复评论", httpMethod = "POST", response = String.class)
    @RequestMapping(value="/reventComment",  method = RequestMethod.POST)
	public ResponseEntity<ResultMessage> reventComment(
	        @ApiParam(name = "[comment]", required = true, value = "评论实体")
	        @RequestParam(name = "comment", required = true) Comment comment) {

        logger.info("<====== 进入reventComment:方法,{[comment] : } ====");
        // desc: 封装操作结果
        ResultMessage result = new ResultMessage();

        result.setServiceResult(commentService.reventComment(comment) != null);
        result.setMessageCode(200l);
		if (result.getServiceResult()) {
			result.setResultInfo("请求操作成功");
		} else {
			result.setResultInfo("请求操作失败");
		}

        logger.info("==== 退出reventComment方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
        return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
    }

	/**
	 * 查看用户的所有评论
	 * @param userId
	 * @return
	 */
	@ApiOperation(value = "查看用户的所有评论", notes = "查看用户的所有评论", httpMethod = "POST", response = String.class)
	@RequestMapping(value="/User/{userId}",  method = RequestMethod.POST)
	public ResponseEntity<ResultMessage> findUserComments(
	        @ApiParam(name = "userId", required = true, value = "用户Id")
	        @RequestParam(name = "userId", required = true)Long userId) {
		logger.info("<====== 进入findByuserId:方法,{[userId] : " + userId + " } ====");

		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		result.getResultParm().put("commentList", commentService.findUserComments(userId));
		result.setMessageCode(200l);
		if (null != result.getResultParm().get("commentList")) {
            result.setResultInfo("请求操作成功");
        } else {
		    result.setResultInfo("请求操作失败");
        }

		logger.info("==== 退出findByuserId方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	/**
	 * 查看商家的所有评论
	 * @param businessId
	 * @return
	 */
	@ApiOperation(value = "获取商家的所有评论", notes = "获取商家的所有评论", httpMethod = "POST", response = String.class)
	@RequestMapping(value="/Business/{businessid}",  method = RequestMethod.POST)
	public ResponseEntity<ResultMessage> findBusinessComments(
	        @ApiParam(name = "businessId", required = true, value = "商家Id")
	        @RequestParam(name = "businessId", required = true)
	        Long businessId) {
		logger.info("<====== 进入findByBusinessId:方法,{[businessId] : " + businessId + " } ====");

		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		result.getResultParm().put("commentList", commentService.findBusinessComments(businessId));
		result.setMessageCode(200l);
        if (null != result.getResultParm().get("commentList")) {
            result.setResultInfo("请求操作成功");
        } else {
            result.setResultInfo("请求操作失败");
        }

		logger.info("==== 退出findByBusinessId方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);

	}
	
}